package com.hmkj.crm.billing.web.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hmkj.crm.billing.domain.relevance.ProductInventoryDetail;
import com.hmkj.crm.billing.domain.relevance.RelProductInventory;
import com.hmkj.crm.billing.dto.Message;
import com.hmkj.crm.billing.dto.ProductInventoryDTO;
import com.hmkj.crm.billing.dto.ProductInventoryDetailDTO;
import com.hmkj.crm.billing.service.relevance.ProductInventoryDetailService;
import com.hmkj.crm.billing.service.relevance.RelProductInventoryService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 库存 控制器
 *
 * @author sks.lwei
 * @date 2020/02/15
 */
@RestController
@RequestMapping("/repertory")
public class RepertoryController {

    @Resource
    private RelProductInventoryService productInventoryService;

    @Resource
    private ProductInventoryDetailService productInventoryDetailService;

    @RequestMapping("/listData")
    public Message listData(Page<RelProductInventory> page, RelProductInventory relProductInventory){
        String productUuid = relProductInventory.getProductUuid();
        String deliveryUuid = relProductInventory.getDeliveryUuid();
        QueryWrapper<RelProductInventory> wrapper = new QueryWrapper<>();
        if(StrUtil.isNotBlank(productUuid)){
            wrapper.eq("product_uuid", productUuid);
        }
        if(StrUtil.isNotBlank(deliveryUuid)){
            wrapper.eq("delivery_uuid", deliveryUuid);
        }
        IPage<ProductInventoryDTO> productInventoryPage = productInventoryService.listProductInventoryPage(page, wrapper);


        return Message.page(productInventoryPage);
    }

    /**
     * 库存产品详情（流水）
     * @param uuid relProductInventory uuid
     * @return {@link Message}
     */
    @RequestMapping("/detail")
    public Message productInventoryDetail(Page<ProductInventoryDetail> page, String uuid, @RequestParam(value = "type", required = false) String inventoryType){
        QueryWrapper<ProductInventoryDetail> wrapper = new QueryWrapper<>();
        wrapper.eq("inventory_uuid", uuid);
        if(StrUtil.isNotBlank(inventoryType)){
            wrapper.eq("inventory_type", inventoryType);
        }
        IPage<ProductInventoryDetailDTO> inventoryDetailPage = productInventoryDetailService.listProductInventoryDetailPage(page, wrapper);
        return Message.page(inventoryDetailPage);
    }
}
